package org.example.service.imp;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.entity.Adminlogin;
import org.example.mapper.AdminLoginMapper;
import org.example.service.AdminLoginService;
import org.example.util.PasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 管理员登录实现
 */
@Service
public class AdminLoginServiceImpl extends ServiceImpl<AdminLoginMapper, Adminlogin> implements AdminLoginService {
    
    @Autowired
    private AdminLoginMapper adminLoginMapper;
    
    @Override
    public Adminlogin login(String account, String password) {
        try {
            // 根据账号查询管理员
            Adminlogin admin = adminLoginMapper.getAdminByAccount(account);
            if (admin == null) {
                return null;
            }
            
            // 检查密码格式
            if (admin.getPassword() == null || admin.getPassword().trim().isEmpty()) {
                return null;
            }
            
            if (!admin.getPassword().startsWith("$2a$")) {
                return null;
            }
            
            // 使用BCrypt验证密码
            boolean isPasswordValid = PasswordEncoder.matches(password, admin.getPassword());
            
            if (isPasswordValid) {
                return admin;
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }
    
    @Override
    @Deprecated
    public Adminlogin getAdminLogin(String account, String password) {
        return adminLoginMapper.getAdminLogin(account, password);
    }
}
